﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace SapB1WebPOS.BLL
{
    public class ReceiptDesign
    {
        public string GetReceiptStr(DataSet dsReceipt)
        {

            string str = "";
            DataTable dtHeader = dsReceipt.Tables[0];
            if (dtHeader.Rows.Count == 0)
            {
                return "";
            }
            for (int ii = 0; ii < dtHeader.Rows.Count; ii++)
            {
                str = str + "HOAN CAU" + System.Environment.NewLine;
                str = str + "NHA TRANG" + System.Environment.NewLine;
                str = str + StrAlign("[RECEIPT]", 0) + System.Environment.NewLine;
                str = str + StrAlign("Receipt No.:" + Convert.ToString(dtHeader.Rows[0]["DocEntry"].ToString()), 0) + System.Environment.NewLine;
                str = str + "Date:" + DateTime.Parse(dtHeader.Rows[0]["DocDate"].ToString()).ToString("MM/dd/yyyy") + System.Environment.NewLine;
                str = str + "Customer:" + dtHeader.Rows[0]["CardName"].ToString() + System.Environment.NewLine;
                str = str + "Item Name     Quantity   Price     Amount " + System.Environment.NewLine;
                str = str + "------------------------------------------" + System.Environment.NewLine;
                DataTable tbLine = dsReceipt.Tables[1];
                double totalQuantity = 0, billTotal = 0, tax = 0, netValue = 0, discount = 0;
                foreach (DataRow r in tbLine.Rows)
                {
                    str = str + r["Dscription"].ToString() + System.Environment.NewLine;
                    str = str + "       " +
                         " ".PadLeft(6 - double.Parse(r["Quantity"].ToString()).ToString("###,###,###,###").Length) +
                       double.Parse(r["Quantity"].ToString()).ToString("###,###,###,###") +
                         " ".PadLeft(9 - double.Parse(r["Price"].ToString()).ToString("###,###,###,###").Length) +
                       double.Parse(r["Price"].ToString()).ToString("###,###,###,###") +
                         " ".PadLeft(11 - double.Parse(r["LineTotal"].ToString()).ToString("###,###,###,###").Length) +
                       double.Parse(r["LineTotal"].ToString()).ToString("###,###,###,###") + System.Environment.NewLine;

                    billTotal += double.Parse(r["LineTotal"].ToString());
                    tax += double.Parse(r["VatSum"].ToString());

                    double disCountAmt = (double.Parse(r["LineTotal"].ToString()) * double.Parse(r["DiscPrcnt"].ToString())) / 100;
                    totalQuantity += double.Parse(r["Quantity"].ToString());
                    discount += disCountAmt;
                }
                netValue = billTotal + tax;

                str = str + "------------------------------------------" + System.Environment.NewLine;
                str = str + StrAlign("Sub Total:" + billTotal.ToString("###,###,###,###"), 1) + System.Environment.NewLine;
                str = str + StrAlign("Tax:" + tax.ToString("###,###,###,###"), 1) + System.Environment.NewLine;

                str = str + StrAlign("Total:" + netValue.ToString("###,###,###,###"), 1) + System.Environment.NewLine;
                str = str + StrAlign("------------------", 1) + System.Environment.NewLine;
                //if (dt.Rows[0]["Cash"] > 0)
                //{
                //    str = str + StrAlign("Cash: " + string.Format(dt.Rows[0]["Cash"], "###,###,###,###"), 1) + System.Environment.NewLine;
                //}
                //if (dt.Rows[0]["CC"] > 0)
                //{
                //    str = str + StrAlign("Credit Card: " + Strings.Format(dt.Rows[0]["CC"], "###,###,###,###"), 1) + System.Environment.NewLine;
                //}
                //if (dt.Rows[0]["Voucher"] > 0)
                //{
                //    str = str + StrAlign("Voucher: " + Strings.Format(dt.Rows[0]["Voucher"], "###,###,###,###"), 1) + System.Environment.NewLine;
                //}
                //if (dt.Rows[0]["Mileage"] > 0)
                //{
                //    str = str + StrAlign("Mileage: " + Strings.Format(dt.Rows[0]["Mileage"], "###,###,###,###"), 1) + System.Environment.NewLine;
                //}
                //if (dt.Rows[0]["Coupon"] > 0)
                //{
                //    str = str + StrAlign("Coupon: " + Strings.Format(dt.Rows[0]["Coupon"], "###,###,###,###"), 1) + System.Environment.NewLine;
                //}
                str = str + "------------------------------------------" + System.Environment.NewLine;
                //str = str + "POS:" + dtHeader.Rows[0]["SlpName"].ToString() + "- Cashier:" + dtHeader.Rows[0]["Cashier"].ToString() + System.Environment.NewLine;
                str = str + "------------------------------------------" + System.Environment.NewLine;
                str = str + StrAlign("Thank you and see you again", 0) + System.Environment.NewLine;
                str = str + (char)(0x1d) + "V" + (char)(66) + (char)(0);
                //CutPaper() 'Chr(29) + Chr(86) + Chr(66) + Chr(255)
            }

            str = str + OpenCashStr();
            return str;
        }
        private string StrAlign(string str, int Align)
        {
            //Align=0: Center
            if (Align == 0)
            {
                return " ".PadLeft((42 - str.Length) / 2) + str;
            }
            else if (Align == 1)
            {
                //Align=1: Right
                return " ".PadLeft(42 - str.Length) + str;
            }
            else
            {
                return str;
            }
        }
        public string OpenCashStr()
        {
            System.Text.StringBuilder sequence = new System.Text.StringBuilder();
            sequence.Append((char)(27));
            sequence.Append((char)(112));
            sequence.Append((char)(0));
            sequence.Append((char)(25));
            sequence.Append((char)(250));
            return sequence.ToString();
        }
    }
}